<?php

namespace app\api\model;

use fast\Http;
use think\Model;
use think\Request;

Class Common extends Model {


    public function __construct($merchant_id = '', $key = '') {
        $this->domain  = Request::instance()->domain();
    }

    public function createNO($table, $field, $prefix)
    {
        {

            $billno = date('YmdHis') . $this->random(6, true);
            while (1) {
                $count = pdo_fetchcolumn('select count(*) from ' . tablename('ewei_shop_' . $table) . " where {$field}=:billno limit 1", array(':billno' => $billno));
                if ($count <= 0) {
                    break;
                }
                $billno = date('YmdHis') . $this->random(6, true);
            }
            return $prefix . $billno;
        }
    }

    function random($length, $numeric = false) {
        $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
        $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
        if ($numeric) {
            $hash = '';
        } else {
            $hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
            --$length;
        }
        $max = strlen($seed) - 1;
        for ($i = 0; $i < $length; ++$i) {
            $hash .= $seed[mt_rand(0, $max)];
        }

        return $hash;
    }


}

